const Koa = require("koa") // 引入Koa
const KoaRouter = require("koa-router") // 引入KoaRouter

const app = new Koa() // 创建Koa实例
const router = new KoaRouter() // 创建KoaRouter实例

// 获取首页初始化数据
const indexData = require("./datas/index.json") // 引入首页数据
router.get("/getIndexData", (ctx, next) => {
	ctx.body = {
		code: 200,
		data: indexData
	}
})

// 主页导航页面数据
let indexCateList = require('./datas/indexCateList.json')
router.get('/getindexCateList', async (ctx, next) => {
	// 生成的H5应用存在跨域问题，通过CORS设置跨域配置
	ctx.set('Access-Control-Allow-Origin', '*')
	await new Promise(resolve => setTimeout(() => resolve(), 2000))
	ctx.body = {
		code: 200,
		data: indexCateList
	};
});

// 分类页面数据 
let cateGoryData = require('./datas/categoryDatas.json');
router.get('/getCategoryDatas', async (ctx, next) => {
	ctx.body = {
		code: 200,
		data: cateGoryData
	};
});


app.use(router.routes()).use(router.allowedMethods())

app.listen(3002, (error) => {
	if (error) {
		console.log("服务器连接失败")
		return
	}
	console.log("服务器连接成功")
})
